import dt from '@dt/dt'
import ruleOpt from '@/biz/option/rule.js'
import toolMgr from '@/biz/base/tool.js'

let _this = null

function clearNoNum(val) {
  return String(val).replace(/[^\d.]/g, '').replace(/\.{2,}/g, '.').replace('.', '$#$')
    .replace(/\./g,
      '').replace('$#$', '.').replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');
}

function selectOption(opt) {
  let gap = {
    placeholder: '-',
    col: 1,
    visible: _this.info[opt.key1] == 2,
    text: {
      textAlign: 'center'
    }
  }
  let start = {
    disabled: _this.is_view,
    key: opt.key2,
    col: 6,
    input: {
      type: 'number'
    },
  }
  let end = {
    disabled: _this.is_view,
    key: opt.key3,
    col: 6,
    visible: _this.info[opt.key1] == 2,
    input: {
      type: 'number',
    }
  }
  let option = {
    key: opt.key1,
    title: opt.title,
    group: [{
      key: opt.key1,
      col: 4,
      disabled: _this.is_view,
      radio: {
        options: [{ key: 1, value: '固定' }, { key: 2, value: '范围' }],
      },
      change: (res) => {
        gap.visible = res.value == 2
        end.visible = res.value == 2
        _this.info[opt.key2] = null
        _this.info[opt.key3] = null
      }
    }, start, gap, end, {
      placeholder: opt.unit,
      col: 3,
      text: {}
    }],
    rules: [
      ruleOpt.check('请填写完整的区间值', () => {
        if (_this.info[opt.key1] == 2) {
          return (_this.info[opt.key3] == null && _this.info[opt.key2] != null) ||
            (_this.info[opt.key3] != null && _this.info[opt.key2] == null)
        }
      }),
      ruleOpt.check('不允许输入负数', () => {
        if (_this.info[opt.key1] == 2) {
          return _this.info[opt.key3] < 0 || _this.info[opt.key2] < 0
        }
      }),
      ruleOpt.check('起始值须小于结束值', () => {
        if (_this.info[opt.key1] == 2) {
          return _this.info[opt.key3] <= _this.info[opt.key2]
        }
      }),
    ]
  }
  return option
}


function floorPic(info) {
  let api = toolMgr.unloadFile()
  let files = []
  if (info.houseTypeAnnex && info.houseTypeAnnex.length > 0) {
    info.houseTypeAnnex.forEach(item => {
      if (item) {
        files.push({
          name: item.name,
          url: item.url,
          cover: item.cover
        })
      }
    })
  }
  return {
    title: '户型图',
    key: 'houseTypeAnnex',
    uploadImg: {
      title: '户型图',
      api,
      files,
      multiple: false,
      hasCover: true,
      trueRadioTxt: '封面图',
      falseRadioTxt: '设置封面图',
      preview: (file) => {
        console.log(file)
        toolMgr.downloadFile(file.url, file.name)
      },
      show: false,
      hideDel: _this.is_view ? true : false,
      maxlength: _this.is_view ? -1 : 30,
      format: ['jpg', 'jpeg', 'png'],
      formatErr: () => {
        _this.$Message.error('只支持上传jpg、jpeg、png图片！')
      },
    },
    data: {
      to: (v) => {
        let houseTypeAnnex = v.map(item => {
          return {
            name: item.name,
            url: item.response ? item.response.data.imageURL : item.url,
            cover: item.cover ? item.cover : 2
          }
        })
        return {
          houseTypeAnnex
        }
      }
    }
  }
}

function options(vm) {
  _this = vm
  let arr = [
    {
      title: '户型名称',
      placeholder: '请输入户型名称',
      key: 'houseTypeName',
      disabled: _this.is_view,
      col: 12,
      input: {
        maxlength: 20
      },
      rules: [{
        required: true,
        message: '户型名称不能为空',
        trigger: 'blur'
      }]
    },
    selectOption({
      title: '层高',
      key1: 'storyHeightOptions',
      key2: 'storyHeightStart',
      key3: 'storyHeightEnd',
      unit: 'm'
    }),
    selectOption({
      title: '开间（门面宽度）',
      key1: 'facadeWidthOptions',
      key2: 'facadeWidthStart',
      key3: 'facadeWidthEnd',
      unit: 'm'
    }),
    {
      title: '是否允许搭建二层',
      key: 'layerTwo',
      col: 12,
      disabled: _this.is_view,
      radio: {
        options: [{ key: '1', value: '允许' }, { key: '2', value: '不允许' }],
      },
    },
    selectOption({
      title: '建面',
      key1: 'floorAreaOptions',
      key2: 'floorAreaStart',
      key3: 'floorAreaEnd',
      unit: '㎡'
    }),
    selectOption({
      title: '单价',
      key1: 'unitPriceOptions',
      key2: 'unitPriceStart',
      key3: 'unitPriceEnd',
      unit: '元/㎡'
    }),
    selectOption({
      title: '总价',
      key1: 'totalPriceOptions',
      key2: 'totalPriceStart',
      key3: 'totalPriceEnd',
      unit: '万元'
    }),
    {
      title: '销售状态',
      key: 'salesStatus',
      col: 12,
      disabled: _this.is_view,
      radio: {
        options: dt.dictInfo().buSalesStatus,
      },
      rules: [{
        required: true,
        message: '销售状态不能为空',
        trigger: 'blur'
      }]
    },
    floorPic(_this.info),
    {
      title: '户型点评',
      key: 'houseTypeComment',
      col: 20,
      disabled: _this.is_view,
      input: {
        type: 'textarea',
        maxlength: 200,
        autosize: {
          minRows: 2,
          maxRows: 5
        }
      },
    }
  ]
  return arr
}

export default {
  options
}
